package cn.tedu.fengyuback.personal.service.impl;

import cn.tedu.fengyuback.personal.mapper.QuestionMapper;
import cn.tedu.fengyuback.personal.pojo.dto.QuestionFollowsQuery;
import cn.tedu.fengyuback.personal.pojo.dto.QuestionQuery;
import cn.tedu.fengyuback.personal.pojo.entity.Question;
import cn.tedu.fengyuback.personal.pojo.vo.QuestionFollowsVO;
import cn.tedu.fengyuback.personal.pojo.vo.QuestionVO;
import cn.tedu.fengyuback.personal.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
@Slf4j
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<QuestionVO> selectQuestion(Long userId) {
        QuestionQuery questionQuery = new QuestionQuery();
        questionQuery.setUserId(userId);
        List<QuestionVO> list = questionMapper.selectQuestion(questionQuery);
//        log.debug("用户发布的问题：{}",list);
        for (QuestionVO questionVO : list){
            followQuery(questionVO);
        }
        return list;
    }

    @Override
    public void updateQuestionsFollowStatus(QuestionFollowsQuery questionFollowsQuery) {
        QuestionFollowsVO questionFollowsVO = questionMapper.selectQuestionFollows(questionFollowsQuery);
        if (questionFollowsVO == null){//用户未关注该问题，进行关注操作
            QuestionFollowsVO questionFollows = new QuestionFollowsVO();
            BeanUtils.copyProperties(questionFollowsQuery,questionFollows);
            questionFollows.setFollowTime(new Date());
            questionMapper.insertQuestionFollows(questionFollows);
            addMinusFollows(questionFollowsQuery, "+");
        }else {//用户已关注该问题，取消关注操作
            questionMapper.deleteQuestionFollows(questionFollowsQuery);
            addMinusFollows(questionFollowsQuery, "-");
        }
    }

    /**
     * 增加/减少问题关注数
     * @param questionFollowsQuery
     */
    private void addMinusFollows(QuestionFollowsQuery questionFollowsQuery, String str) {
        Question question = new Question();
        Long num = null;
        if (str.equals("+")){
            num = questionFollowsQuery.getQuestionsFollows() + 1;
        }else {
            num = questionFollowsQuery.getQuestionsFollows() - 1;
        }
        question.setQuestionsFollows(num.toString());
        question.setId(questionFollowsQuery.getQuestionId());
        questionMapper.updateQuestion(question);
    }

    /**
     * 查询用户是否关注了问题
     * @param questionVO
     */
    private void followQuery(QuestionVO questionVO) {
        QuestionFollowsQuery questionFollowsQuery = new QuestionFollowsQuery();
        questionFollowsQuery.setQuestionId(questionVO.getId());
        questionFollowsQuery.setUserId(questionVO.getUserId());
//        log.debug("问题id={},用户id={}",questionFollowsQuery.getQuestionId(),questionFollowsQuery.getUserId());
        QuestionFollowsVO questionFollowsVO = questionMapper.selectQuestionFollows(questionFollowsQuery);
        log.debug("问题关注状态：{}",questionFollowsVO);
        if (questionFollowsVO != null){
            questionVO.setQuestionsFollowStatus("primary");
        }else {
            questionVO.setQuestionsFollowStatus("");
        }
    }
}
